package android.pot.com.smartcar.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.util.Random;

/**
 * Created by zhaoshilu on 2017/7/17.
 */

public class Utils {
    private static Toast toast;
    private static long lastClickTime = 0;

    /**
     * log输出
     *
     * @param Tag（标签） 默认“onResponse”
     * @param str（内容）
     */
    public static void showLog(String Tag, String str) {

        if (Utils.isNull(Tag)) {
            Tag = "onResponse";
        }
        Log.i(Tag, str);

    }

    /**
     * Toast 默认短时间显示
     *
     * @param context 字符串
     */
    public static void showToast(Context context, String text) {
        if (Utils.isNull(text)) {
            return;
        }
        if (toast == null) {
            toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        } else {
            toast.setText(text);
        }
        toast.show();
    }

    /**
     * Toast 默认短时间显示
     *
     * @param context
     * @param resId   字符串ID
     */
    public static void showToast(Context context, int resId) {
        if (toast == null) {
            toast = Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT);
        } else {
            toast.setText(context.getResources().getString(resId));
        }
        toast.show();
    }

    /**
     * 判断字符串是否为空
     *
     * @param str
     * @return 为空返回true 不为空返回false
     */
    public static boolean isNull(String str) {
        if (str != null && !str.equals("")) {
            return false;
        }
        return true;
    }

    /**
     * 获取屏幕尺寸
     */
    public static int getScreenWidth(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        return display.getWidth();

    }


    /**
     * 递归删除文件和文件夹
     *
     * @param file 要删除的根目录
     */
    public static void deleteFile(File file) {
        if (file.exists() == false) {
            // mHandler.sendEmptyMessage(0);
            return;
        } else {
            if (file.isFile()) {
                file.delete();
                return;
            }
            if (file.isDirectory()) {
                File[] childFile = file.listFiles();
                if (childFile == null || childFile.length == 0) {
                    file.delete();
                    return;
                }
                for (File f : childFile) {
                    deleteFile(f);
                }
                file.delete();
            }
        }
    }

    /**
     * 判断是否连续点击
     *
     * @param time 时间差（毫秒）
     * @return 连续点击返回 true
     */
    public static boolean isFastDoubleClick(long time) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - lastClickTime < time) {
            return true;
        }
        lastClickTime = nowTime;
        return false;
    }

    /**
     * 判断网络是否连接
     *
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();

            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 设置EditText只能输入两位小数
     *
     * @param editText
     */
    public static void setPricePoint(final EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().contains(".")) {
                    if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0, s.toString().indexOf(".") + 3);
                        editText.setText(s);
                        editText.setSelection(s.length());
                    }
                }
                if (s.toString().trim().substring(0).equals(".")) {
                    s = "0" + s;
                    editText.setText(s);
                    editText.setSelection(2);
                }

                if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        editText.setText(s.subSequence(0, 1));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
            }
        });

    }

    /**
     * 获得指定区间的随机数
     *
     * @param min 最小值
     * @param max 最大值
     * @return
     */
    public static int getRandom(int min, int max) {
        //得到min-(max+1)的整数
        Random random = new Random();
        return random.nextInt(max - min + 1) + min;
    }
}
